package com.dc.di.sf.dt;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.axiom.om.OMElement;

import com.dc.di.sf.utils.OMUtils;

public class DataStructure implements DTObject<DataStructure> {

	public static final String TAG_DATASTRUCTURE = "DataStructure";
	public static final String TAG_IDENTIFIER = "Identifier";
	public static final String TAG_DISPLAYNAME = "DisplayName";
	public static final String TAG_EXPLANATORYCOMMENT = "ExplanatoryComment";

	private String identifier;
	private String displayName;
	private String explanatoryComment;
	private List<DataUnit> dataUnits = new ArrayList<DataUnit>();
	private List<ExtendAttribute> extendAttributes = new ArrayList<ExtendAttribute>();

	public DataStructure() {

	}

	/**
	 * @return the identifier
	 */
	public String getIdentifier() {
		return identifier;
	}

	/**
	 * @param identifier
	 *            the identifier to set
	 */
	public void setIdentifier(String identifier) {
		this.identifier = identifier;
	}

	/**
	 * @return the displayName
	 */
	public String getDisplayName() {
		return displayName;
	}

	/**
	 * @param displayName
	 *            the displayName to set
	 */
	public void setDisplayName(String displayName) {
		this.displayName = displayName;
	}

	/**
	 * @return the explanatoryComment
	 */
	public String getExplanatoryComment() {
		return explanatoryComment;
	}

	/**
	 * @param explanatoryComment
	 *            the explanatoryComment to set
	 */
	public void setExplanatoryComment(String explanatoryComment) {
		this.explanatoryComment = explanatoryComment;
	}

	/**
	 * @return the dataUnits
	 */
	public List<DataUnit> getDataUnits() {
		return dataUnits;
	}

	/**
	 * @param dataUnits
	 *            the dataUnits to set
	 */
	public void setDataUnits(List<DataUnit> dataUnits) {
		this.dataUnits = dataUnits;
	}

	/**
	 * @return the extendAttribute
	 */
	public List<ExtendAttribute> getExtendAttributes() {
		return extendAttributes;
	}

	/**
	 * @param extendAttribute
	 *            the extendAttribute to set
	 */
	public void setExtendAttributes(List<ExtendAttribute> extendAttributes) {
		this.extendAttributes = extendAttributes;
	}

	public OMElement toXML() {
		OMElement structure = OMUtils.createOMElement(TAG_DATASTRUCTURE);
		structure.addChild(OMUtils.createOMElementWithText(TAG_IDENTIFIER, identifier));
		structure.addChild(OMUtils.createOMElementWithText(TAG_DISPLAYNAME, displayName));
		structure.addChild(OMUtils.createOMElementWithText(TAG_EXPLANATORYCOMMENT, explanatoryComment));
		for (DataUnit dataUnit : dataUnits)
			structure.addChild(dataUnit.toXML());
		for (ExtendAttribute attribute : extendAttributes)
			structure.addChild(attribute.toXML());
		return structure;
	}

	public DataStructure fromXML(OMElement xml) {
		if (xml != null) {
			identifier = OMUtils.getTextOfChild(xml, TAG_IDENTIFIER);
			displayName = OMUtils.getTextOfChild(xml, TAG_DISPLAYNAME);
			explanatoryComment = OMUtils.getTextOfChild(xml, TAG_EXPLANATORYCOMMENT);
			for (Iterator itr = OMUtils.getChildren(xml, DataUnit.TAG_DATAUNIT); itr.hasNext();) {
				dataUnits.add(new DataUnit((OMElement) itr.next()));
			}
			for (Iterator itr = OMUtils.getChildren(xml, ExtendAttribute.TAG_EXTENDATTRIBUTE); itr.hasNext();) {
				extendAttributes.add(new ExtendAttribute((OMElement) itr.next()));
			}
		}
		return this;
	}

	/* (non-Javadoc)
	 * @see com.dc.di.sf.dt.DTObject#toXMLString()
	 */
	public String toXMLString() {
		StringBuilder sb = new StringBuilder();
		sb.append(OMUtils.generateOMElementBeginString(TAG_DATASTRUCTURE));
		sb.append(OMUtils.generateOMElementString(TAG_IDENTIFIER, identifier));
		sb.append(OMUtils.generateOMElementString(TAG_DISPLAYNAME, displayName));
		sb.append(OMUtils.generateOMElementString(TAG_EXPLANATORYCOMMENT, explanatoryComment));
		for (DataUnit dataUnit : dataUnits)
			sb.append(dataUnit.toXMLString());
		for (ExtendAttribute attribute : extendAttributes)
			sb.append(attribute.toXMLString());
		sb.append(OMUtils.generateOMElementEndString(TAG_DATASTRUCTURE));
		return sb.toString();
	}

	public void addDataUnit(DataUnit dataUnit) {
		dataUnits.add(dataUnit);
	}

	public void removeDataUnit(int index) {
		dataUnits.remove(index);
	}

	public void removeDataUnit(String idName) {
		int i = indexOfDataUnit(idName);
		if (i >= 0)
			removeDataUnit(i);
	}

	public int indexOfDataUnit(String idName) {
		for (int i = 0; i < dataUnits.size(); i++)
			if (dataUnits.get(i).getIdName().equalsIgnoreCase(idName))
				return i;
		return -1;
	}

}
